package org.adream.account.service;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.adream.account.dao.ResourceDao;
import org.adream.account.entity.ResourceEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.util.Constant;
import org.adream.account.util.IdGenerator;
import org.adream.account.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class ResourceService {
	private final static Logger logger = LoggerFactory.getLogger(ResourceService.class);
	
	@Autowired
	private ResourceDao resourceDao;
	
	@Autowired
	private HttpSession session;
	
	/**
	 * 增加权限
	 * @param resource
	 */
	public ResultModel<String> addResource(ResourceEntity resource) {
		String resId = IdGenerator.getRandomFour(Constant.FOUR);
		resource.setResId(resId);
		String modifier = (String)session.getAttribute("uid");
		resource.setCreator(modifier);
		resource.setCts(new Date());
		resource.setModifier(modifier);
		resource.setMts(new Date());
		resource.setDr(Constant.ONE);
		int result = resourceDao.addResource(resource);
		if(result == 0) {
			return new ResultModel<String>(ResultModel.ERR_DATABASE, "系统错误,请联系系统管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
	}
	
	/**
	 * 删除权限  逻辑删除
	 * @param resId   resource id
	 */
	public ResultModel<String> deleteResource(String resId) {
		ResourceEntity resource = resourceDao.queryResourceByDrAndResId(Constant.ONE, resId);
		if(resource == null) {
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "删除失败,数据不存在", null);
		}
		String modifier = (String)session.getAttribute("uid");
		resource.setModifier(modifier);
		resource.setMts(new Date());
		resource.setDr(Constant.ZERO);
		int result = resourceDao.deleteResource(resource);
		if(result == 0) {
			return new ResultModel<String>(ResultModel.ERR_DATABASE, "系统错误,请联系系统管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
	}
	
	/**
	 * 更新权限
	 * @param resource
	 */
	public ResultModel<String> updateResource(ResourceEntity resource) {
		ResourceEntity resourceEntity = resourceDao.queryResourceByResId(resource.getResId());
		if(resourceEntity == null) {
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "更新失败,数据不存在", null);
		}
		String modifier = (String)session.getAttribute("uid");
		resource.setCreator(modifier);
		resource.setMts(new Date());
		Utils.copyPropertiesIgnoreNull(resource, resourceEntity);
		int result = resourceDao.updateResource(resource);
		if(result == 0) {
			return new ResultModel<String>(ResultModel.ERR_DATABASE, "系统错误,请联系系统管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
	}
	
	/**
	 * 根据删除标志查询resource
	 * @param dr 删除标志
	 * @return
	 */
	public List<ResourceEntity> queryResourcesByDr(Integer dr) {
		return resourceDao.queryResourcesByDr(dr);
	}
	
	/**
	 * 通过dr、resource name模糊查询Resource
	 * @param dr 删除标志
	 * @param resName resource name
	 * @return
	 */
	public List<ResourceEntity> queryResByDrAndLikeResName(Integer dr,String resName) {
		return resourceDao.queryResByDrAndLikeResName(dr, resName);
	}
	
	/**
	 * 根据like %resName%分页查询Resource
	 * @param dr	删除标志
	 * @param resName	resource name
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public ResultModel<?> selectPageReses(Integer dr, String resName, Integer pageNo, Integer pageSize) {
		PageHelper.startPage(pageNo, pageSize);
		List<ResourceEntity> resources = resourceDao.queryResByDrAndLikeResName(dr, resName);
		if(Utils.isEmptyCollection(resources)) {
			logger.warn("查询出错,数据不存在:(");
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "查询出错,数据不存在", null);
		}
		PageInfo<ResourceEntity> page = new PageInfo<>(resources, Constant.TEN);
		return new ResultModel<PageInfo<ResourceEntity>>(ResultModel.SUCCESS, "查询成功", page);
	}
	
	/**
	 * 通过resId查询Resource
	 * @param resId resource id
	 * @return
	 */
	public ResourceEntity queryResourceByResId(String resId) {
		return resourceDao.queryResourceByResId(resId);
	}
	
	/**
	 * 根据删除标志和resource id查询
	 * @param dr 删除标志
	 * @param resId resource id
	 * @return
	 */
	public ResourceEntity queryResourceByDrAndResId(int dr,String resId) {
		return resourceDao.queryResourceByDrAndResId(dr, resId);
	}
	
	/**
	 * 权限名是否已存在
	 * @param resName resource name
	 * @return
	 */
	public boolean isValidResName(String resName) {
		boolean isValidResName = false;
		ResourceEntity resource = resourceDao.queryResourceByResName(resName);
		if(!StringUtils.isEmpty(resName) && resource == null) {
			isValidResName = true;
		}
		return isValidResName;
	}
	
	/**
	 * 根据权限id，类型查询
	 * @param resIds
	 * @param type
	 * @return
	 */
	public List<ResourceEntity> queryResourceByResIds(String[] resIds,String type) {
		if(resIds == null || resIds.length == 0) {
			return null;
		}
		return resourceDao.queryResourceByResIds(resIds,type);
	}
}